//
//  ViewController.m
//  Demo
//
//  Created by seayu on 2017/5/15.
//  Copyright © 2017年 seayu. All rights reserved.
//

#import "ViewController.h"
#import "BoxStickerView.h"
#import "TwoVC.h"



@interface ViewController ()<BoxStickerViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) BoxStickerView *currentSelectedBoxStickerView;
@property (weak, nonatomic) UITextField *textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _bgView.clipsToBounds = YES;
    
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgViewDidTap)];
    [_bgView addGestureRecognizer:tap];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    UITextField *textField = [[UITextField alloc]init];
    textField.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 35);
    textField.backgroundColor = [UIColor blueColor];
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    [self.view addSubview:textField];
    textField.delegate = self;
    _textField = textField;
    
}



- (void)keyboardWillChangeFrame:(NSNotification *)notification{

    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat keyboardY = keyboardFrame.origin.y;
    CGFloat keyboardHeight = keyboardFrame.size.height;
    NSInteger animationOption = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
    
    CGFloat timeInterval = [[notification.userInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
    
    CGRect textFieldFrame = self.textField.frame;
    if (keyboardY  == screenHeight) {
        textFieldFrame.origin.y = keyboardY;
    }else{
        textFieldFrame.origin.y = screenHeight - keyboardHeight - textFieldFrame.size.height;
    }
    [UIView animateWithDuration:timeInterval delay:0.0 options:animationOption << 16  animations:^{
        _textField.frame = textFieldFrame;
    } completion:nil];
}

- (IBAction)add:(id)sender {
    
    UIImage *image = [UIImage imageNamed:@"222"];
    // {{0, 0}, {259, 148}}
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    CGRect contentLabelFrame = CGRectMake((259 - 209) / 2,(148 - 100) /2 , 209, 100);
    
    
    BoxStickerView *box1 = [[BoxStickerView alloc]initWithFrame:frame contentLabelFrame:contentLabelFrame contentImage:image];
    box1.center = _bgView.center;
    box1.enabledRightTopControl = NO;
    box1.enabledLeftBottomControl = NO;
    box1.delegate = self;
    [_bgView addSubview:box1];
    self.currentSelectedBoxStickerView = box1;

}
- (IBAction)changeLabelText:(id)sender {
    NSString *oringalText = self.currentSelectedBoxStickerView.contentLabel.text;
    NSString *text = [NSString stringWithFormat:@"%@%@",oringalText,@"👌Tt中文"];
    if (text.length > 50) {
        return;
    }
    self.currentSelectedBoxStickerView.contentLabel.text = text;
}
- (IBAction)save:(id)sender {
    self.currentSelectedBoxStickerView.selected = NO;
    self.currentSelectedBoxStickerView = nil;
    UIImage *image = [self makeImageWithView:_bgView withSize:_bgView.frame.size];

    TwoVC *nextVC = [[TwoVC alloc]init];
    
    [self presentViewController:nextVC animated:YES completion:^{
        nextVC.imageView.image = image;
        NSData *data = UIImagePNGRepresentation(image);
        
        NSString *file = [NSString stringWithFormat:@"%@/%zd.png",@"/Users/morning/Desktop/生成png的图片的测试/tupian",[[NSDate date] timeIntervalSince1970]];
        [data writeToFile:file atomically:YES];
        
    }];
    
    
    
}
- (void)stickerViewDidTapContentView:(BoxStickerView *)stickerView{
    if (stickerView == self.currentSelectedBoxStickerView) {
        self.textField.text = stickerView.contentLabel.text;
        [self.textField becomeFirstResponder];
        return;
    }
    [self.textField resignFirstResponder];
    self.currentSelectedBoxStickerView = stickerView;
}
- (void)setCurrentSelectedBoxStickerView:(BoxStickerView *)currentSelectedBoxStickerView{
    _currentSelectedBoxStickerView.selected = NO;
    _currentSelectedBoxStickerView = currentSelectedBoxStickerView;
    _currentSelectedBoxStickerView.selected = YES;
}

- (void)bgViewDidTap{
    [self.textField resignFirstResponder];
    self.currentSelectedBoxStickerView = nil;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text.length) {
        self.currentSelectedBoxStickerView.contentLabel.text = textField.text;
    }
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark 生成image
- (UIImage *)makeImageWithView:(UIView *)view withSize:(CGSize)size
{
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    CGFloat screenScale = [UIScreen mainScreen].scale;
    UIGraphicsBeginImageContextWithOptions(size, NO, screenScale);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}
@end
